package com.young.dynamicexcel.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.young.dynamicexcel.common.dto.DynamicExcelParam;
import com.young.dynamicexcel.common.util.DynamicExcelUtil;
import com.young.dynamicexcel.common.util.SpringContextUtil;
import com.young.dynamicexcel.service.DynamicExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;

/**
 * @DESCRIPTION: excel动态导入导出service实现类
 * @USER: Young
 */
@Service
public class DynamicExcelServiceImpl implements DynamicExcelService {

    @Autowired
    private DataSource dataSource;

    @Override
    public void exportExcelByDatabase(String tableName, String fileName, boolean isTemplate, HttpServletRequest request, HttpServletResponse response) throws Exception {

        DynamicExcelUtil.createExcelByDatabase(tableName, fileName, isTemplate, dataSource, request, response);

    }

    @Override
    public void importExcelByDatabase(String tableName, InputStream fileInputStream) throws Exception {

        DynamicExcelUtil.insertExcelDataByDatabase(tableName, fileInputStream, dataSource);
    }

    @Override
    public void exportExcelByEntity(DynamicExcelParam dynamicExcelParam, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String entityName = dynamicExcelParam.getEntityName();
        String excelName = dynamicExcelParam.getFileName();
        Map<String, String> headMap =  dynamicExcelParam.getHeadMap();
        ServiceImpl service = getEntityService(entityName);
        if(service==null){
            throw new Exception(String.format("未找到%s的service实现类", entityName));
        }
        List dataList = dynamicExcelParam.getDatas();
        if(dataList.isEmpty()){
            //前端不传数据就从数据库查
            dataList = service.list();
        }

        DynamicExcelUtil.createExcelByEntityData(excelName,headMap,dataList,request,response);
    }

    @Override
    public void importExcelByEntity(String entityName, InputStream fileInputStream) throws Exception {
        ServiceImpl service = getEntityService(entityName);
        if(service==null){
            throw new Exception(String.format("未找到%s的service实现类", entityName));
        }
        List entityDatas = DynamicExcelUtil.convertExcelDataByEntity(getEntityClass(service), fileInputStream);
        service.saveOrUpdateBatch(entityDatas,entityDatas.size());
    }


    private static Class getEntityClass(ServiceImpl service) {

        //service是代理类，父类是具体serviceimpl，再取父类才是ServiceImpl
        Type genericSuperclass = service.getClass().getSuperclass().getGenericSuperclass();
        ParameterizedType paramType = (ParameterizedType) genericSuperclass;
        //获取泛型类型
        Type[] actualTypeArguments = paramType.getActualTypeArguments();

        return (Class) actualTypeArguments[1];
    }

    private ServiceImpl getEntityService(String entityName){
        ServiceImpl service = null;
        try {
            service = (ServiceImpl) SpringContextUtil.getBean(entityName+"ServiceImpl");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return service;
    }

}
